﻿using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations.Schema;
using System.ComponentModel;
using System.Data.Common.CommandTrees.ExpressionBuilder;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using NPOI.SS.UserModel;
using NPOI.HSSF.UserModel;
using NPOI.XSSF.UserModel;

namespace Gx.Common
{
    public static class ExcelHelper
    {
        public static byte[] ExportToExcel<T>(List<T> data, string sheetName, List<string> headers = null)
        {
            byte[] resultByte = null;
            sheetName = !string.IsNullOrWhiteSpace(sheetName) ? sheetName : "Sheet1";
            IWorkbook workbook = new HSSFWorkbook();//创建Workbook对象
            ISheet sheet = workbook.CreateSheet(sheetName);

            //填充表头
            IRow dataRow = sheet.CreateRow(0);
            var columnsPropertyInfo = typeof(T).GetProperties()
                .Where(x => GetAttribute<DisplayNameAttribute>(x) != null).ToArray();//筛选出需要导出的字段;;
            if (headers == null)
            {
                for (int i = 0; i < columnsPropertyInfo.Count(); i++)
                {
                    //object obj = ps[i].GetValue(typeof(T), null);
                    string name = columnsPropertyInfo[i].Name;
                    dataRow.CreateCell(i).SetCellValue(name);
                }
            }
            else
            {
                for (int i = 0; i < headers.Count(); i++)
                {
                    string name = headers[i];
                    dataRow.CreateCell(i).SetCellValue(name);
                }
            }

            //string[] datearr= new string[] { "1911-01-01 0:00:00", "1911/01/01 0:00:00", "1911-1-1 0:00:00", "1911/1/1 0:00:00" };
            //item
            int rowCount = 1;
            foreach (T item in data)
            {
                IRow row = sheet.CreateRow(rowCount);
                int index = 0;
                var propertyInfos = PropertyInfoCache<T>.PropertyInfoArr.Where(p => p.CanRead);
                foreach (PropertyInfo propertyInfo in propertyInfos)
                {
                    string obj = propertyInfo.GetValue(item, null) == null ? "" : propertyInfo.GetValue(item, null).ToString();
                    if (obj.Contains("1911-01-01") || obj.Contains("1911/01/01") || obj.Contains("1911/1/1") || obj.Contains("1911/01/01"))
                        obj = "";
                    row.CreateCell(index).SetCellValue(obj);
                    index++;

                }
                rowCount++;
            }


            //保存
            using (MemoryStream ms = new MemoryStream())
            {
                workbook.Write(ms);
                resultByte = ms.ToArray();
                ms.Dispose();
            }

            return resultByte;
        }



        /// <summary>
        /// 获取定义的特性值
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="provider"></param>
        /// <returns></returns>
        private static T GetAttribute<T>(ICustomAttributeProvider provider)
            where T : Attribute
        {
            return provider.GetCustomAttributes(typeof(T), true)
                .Cast<T>().ToArray().FirstOrDefault();
        }
    }
}
